package de.caeci.iserv;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class IservVerbindung {

	private String vertretungsplanMorgen = "https://www.caeci-ol.de/idesk/caeci/displayinsert.php?filename=foyermorgen.html";
	public Cookie cookies = null;

	private void anmelden(String username, String pwd) {
		try {
			URL url = new java.net.URL("https://www.caeci-ol.de/idesk/");

			String urlParameters = "login_act="
					+ URLEncoder.encode(username, "UTF-8") + "&login_pwd="
					+ URLEncoder.encode(pwd, "UTF-8");
			TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
					return null;
				}

				public void checkClientTrusted(
						java.security.cert.X509Certificate[] certs,
						String authType) {
				}

				public void checkServerTrusted(
						java.security.cert.X509Certificate[] certs,
						String authType) {
				}
			} };

			// Install the all-trusting trust manager
			try {
				SSLContext sc = SSLContext.getInstance("SSL");
				sc.init(null, trustAllCerts, new java.security.SecureRandom());
				HttpsURLConnection.setDefaultSSLSocketFactory(sc
						.getSocketFactory());
			} catch (Exception e) {
			}

			HttpURLConnection connection = (HttpsURLConnection) url
					.openConnection();
			connection.setInstanceFollowRedirects(false);

			connection.setRequestMethod("POST");
			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");

			connection.setRequestProperty("Content-Length",
					"" + Integer.toString(urlParameters.getBytes().length));

			connection.setUseCaches(false);
			connection.setDoInput(true);
			connection.setDoOutput(true);

			// Send request
			DataOutputStream wr = new DataOutputStream(
					connection.getOutputStream());
			wr.writeBytes(urlParameters);
			wr.flush();
			wr.close();
			connection.disconnect();
			// Get response
			connection.setDoOutput(true);
			cookies = new Cookie(connection);
		} catch (Exception e) {
		}
	}

	public String getQuelltext(String url) throws Exception {

		URL actionURL = new URL(url);
		URLConnection conn = actionURL.openConnection();

		conn.addRequestProperty("Cookie", cookies.toString());
		InputStream inStream = conn.getInputStream();

		String content = readInputStream(inStream);

		return content;

	}

	private String readInputStream(InputStream calInStream) {
		BufferedReader br = new BufferedReader(new InputStreamReader(
				calInStream));
		StringBuffer result = new StringBuffer();
		String line;
		try {
			while ((line = br.readLine()) != null) {
				result.append(line);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result.toString();
	}

	public static void main(String[] args) {
		IservVerbindung iservVerbindung = new IservVerbindung();
		iservVerbindung.anmelden("jonas.sitzmann", "ichbindoof124");
		String quelltext;
		try {
			quelltext = iservVerbindung
					.getQuelltext(iservVerbindung.vertretungsplanMorgen);
			System.out.println(quelltext);
		} catch (Exception e) {
		}
	}

}
